from flask import Flask, redirect, url_for, request, render_template

from apps.article.view import article_bp
from apps.user.view import user_bp
from config import DevelopmentConfig
from extend import db, bootstrap, cache

redis_config = {
    'CACHE_TYPE': 'redis',
    'CACHE_REDIS_HOST': '127.0.0.1',
    'CACHE_REDIS_POST': 6379
}


def create_app():
    # 修改默认模板、静态目录，因为app创建位置已经不是和原有模板和静态目录同一级文件
    app = Flask(__name__, template_folder='../templates', static_folder='../static')
    app.config.from_object(DevelopmentConfig)  # 导入配置文件
    db.init_app(app=app)  # 初始化数据库
    bootstrap.init_app(app=app)  # 初始化bootstrap
    app.register_blueprint(user_bp)  # 注册蓝图
    app.register_blueprint(article_bp)
    # 初始化缓存文件
    cache.init_app(app=app, config=redis_config)

    # print(app.url_map)
    @app.route('/')
    def index():
        return redirect(url_for('user.index'))

    return app
